package com.tuanzi.loan.web.bean.family;

import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.LoginUser;
import com.tuanzi.loan.business.repository.family.FirstPartyPrepaymentRepository;
import com.tuanzi.loan.business.service.family.FirstPartyPrepaymentService;
import com.tuanzi.loan.vo.request.family.BusinessPrepaymentSearchRequest;
import com.tuanzi.loan.vo.response.family.BusinessPrepaymentSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.BusinessPrepaidDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 商务充值
 * @author JinRuiyang 2017/11/30
 */
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class BusinessPrepaidBean extends JSFListingBean<BusinessPrepaymentSearchRequest, BusinessPrepaymentSearchResponse> {

    @Autowired
    private FirstPartyPrepaymentService firstPartyPrepaymentService;

    public void onCreateTarget(){
        $.setModelId(null);
        $.setPartnerName(null);
        $.setAmountPerClick(null);
        $.setRechargeBalance(0d);
    }

    public void onRechargeTarget(BusinessPrepaymentSearchResponse response){
        $.setModelId(response.getModelId());
        $.setPartnerName(response.getPartnerName());
        $.setAmountPerClick(response.getAmountPerClick());
        $.setStatus(response.getStatus());
        if (response.isDisable())
            addInfoMessage("您需先启用改商务充值信息才能进行充值或者编辑操作!");
        $.setBalance(firstPartyPrepaymentService.getPrepaymentBalance(response.getModelId()));
        $.setRechargeBalance(0d);
    }

    public void onEditTarget(BusinessPrepaymentSearchResponse response){
        $.setModelId(response.getModelId());
        $.setPartnerName(response.getPartnerName());
        $.setAmountPerClick(response.getAmountPerClick());
    }

    public void onDisable(Long modelId){
        try {
            if (firstPartyPrepaymentService.disable(modelId) == 1)
                addSuccessfullyMessage();
        }
        catch (Exception e){
            addErrorMessage("禁用商务充值信息失败！\n"+e.getMessage());
        }
    }
    public void onEnable(Long modelId){
        try {
            if (firstPartyPrepaymentService.enable(modelId) == 1)
                addSuccessfullyMessage();
        }
        catch (Exception e){
            addErrorMessage("启用商务充值信息失败！\n"+e.getMessage());
        }
    }

    public void onBatchDisable(){
        try {
            firstPartyPrepaymentService.batchDisable(getSelectModelIds());
            addSuccessfullyMessage();
        }
        catch (Exception e){
            addErrorMessage("批量禁用商务充值信息失败！\n"+e.getMessage());
        }
    }
    public void onBatchEnable(){
        try {
            firstPartyPrepaymentService.batchEnable(getSelectModelIds());
            addSuccessfullyMessage();
        }
        catch (Exception e){
            addErrorMessage("批量启用商务充值信息失败！\n"+e.getMessage());
        }
    }

    public String statusConvert(Integer status){
        switch (status){
            case 0:
                return "禁用";
            case 1:
                return "可用";
        }
        return "Unknown";
    }

    private List<Long> getSelectModelIds(){
        List<Long> modelIds = new ArrayList<>();
        if (Objects.nonNull(selectedRecords)){
            for (BusinessPrepaymentSearchResponse response : selectedRecords){
                modelIds.add(response.getModelId());
            }
        }
        System.out.println(modelIds.size());
        return modelIds;
    }

    public void onCreate(){
        try {
            if (firstPartyPrepaymentService.create($) == 1)
                addSuccessfullyMessage();
        }
        catch (Exception e){
            addErrorMessage("创建商务充值信息失败！\n"+e.getMessage());
        }
    }

    public void onEdit(){
        try {
            if (firstPartyPrepaymentService.update($) == 1)
                addSuccessfullyMessage();
        }
        catch (Exception e){
            addErrorMessage("更新商务充值信息失败！\n"+e.getMessage());
        }
    }

    public void onRecharge(){
        try {
            if (firstPartyPrepaymentService.recharge($) == 1)
                addSuccessfullyMessage();
        }
        catch (Exception e){
            addErrorMessage("充值失败！\n"+e.getMessage());
        }
    }

    @PostConstruct
    private void init() {
        initLazyDataModel();
    }

    public void onReset() {
        initLazyDataModel();
    }

    public void onRefresh(){
        lazyDataModel = new BusinessPrepaidDataModel($);
        addSuccessfullyMessage();
    }

    private void initLazyDataModel() {
        $ = new BusinessPrepaymentSearchRequest();
        $.setStatus(1);
        LoginUser user = CurrentUser.get();
        $.setOperatorName(Objects.nonNull(user) ? user.getName() : "UnKnown" );
        lazyDataModel = new BusinessPrepaidDataModel($);
    }
}
